package com.application.tourapp;

import com.application.tourapp.dal.FavoritesDAL;
import com.application.tourapp.dal.TourItemDAL;
import com.application.tourapp.model.Item;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ItemDetailsActivity extends GenericActivity {	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.item_details);
		setHeader(getResources().getString(R.string.Det), true, true);
		Intent intent = getIntent();
        String itemName = intent.getStringExtra("name_item");
        String itemDescription = intent.getStringExtra("item_description");
        final String itemPhoneNumber = intent.getStringExtra("item_number");
        final String itemWebsite = intent.getStringExtra("item_site");
        final String itemLng = intent.getStringExtra("item_lng");
        final String itemLat = intent.getStringExtra("item_lat");
        final String itemId = intent.getStringExtra("item_id");
        
        TextView itemTitleView = (TextView) findViewById(R.id.itemTitle);
        TextView itemDescriptionView = (TextView) findViewById(R.id.itemDescription);
        Button buttonSite = (Button)findViewById(R.id.buttonSite);
        Button buttonAppler = (Button)findViewById(R.id.buttonAppler);
        Button buttonMap = (Button)findViewById(R.id.buttonMap);
        Button buttonFav = (Button)findViewById(R.id.buttonFav);
        
        itemTitleView.setText(itemName);
        itemDescriptionView.setText(itemDescription);
        
        buttonSite.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				String url = "www.google.com";
				if (!url.startsWith("http://") || !url.startsWith("https://")) {
					url = "http://" + url;
				}
				Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
			    startActivity(browse);
				
			}
		});
        
        buttonAppler.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent callIntent = new Intent(Intent.ACTION_CALL);
			    callIntent.setData(Uri.parse("tel:" + itemPhoneNumber));
			    startActivity(callIntent);
			}
		});
        
        buttonMap.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {

				Intent itemMapScreenIntent = new Intent(getApplicationContext(), ItemMapActivity.class);
                ItemsGroupActivity parentActivity = (ItemsGroupActivity)getParent();
                itemMapScreenIntent.putExtra("lng_item", itemLng);
                itemMapScreenIntent.putExtra("lat_item", itemLat);
                parentActivity.startChildActivity("ItemMapActivity", itemMapScreenIntent);

				
			}
		});
        
        buttonFav.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Item item = new TourItemDAL(getApplicationContext()).getTourItem(Integer.parseInt(itemId));
				new FavoritesDAL(getApplicationContext()).addFavorit(item);
			}
		});
	}
}
